package com.ultimate.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ultimate.management.entity.LoginUser;
import com.ultimate.management.entity.ResponseResult;
import com.ultimate.management.entity.SysUser;
import com.ultimate.management.mapper.SysUserMapper;
import com.ultimate.management.service.SysUserService;
import com.ultimate.management.util.response.JwtResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 管理员登录
 *
 * @Author LH
 * @Date 2024/1/16 18:35
 * @PackageName com.ultimate.management.controller
 * @Description: [项目名小写 多个单词用_分隔 | 包名全部小写 | 类 首字母大写 | 变量和方法 首字母小写，多个单词组成时除首字母，其他字母首字母需大写 | 常量 全部大写,多个单词用'_'分割]
 */
@RequestMapping("/user")
@RestController
public class SysUserController {


    //操作系统用户信息
    @Autowired
    private SysUserService sysUserService;

    //使用自带分页
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JwtResponse jwtResponse; //响应信息




    @GetMapping("/info")
    public JwtResponse info(HttpServletRequest request) {

        //从上下文中获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        if(loginUser == null ){
            return jwtResponse.error();
        }

        SysUser sysUser = loginUser.getUser();

        //获取数据库中用户名和头像
        Map<String, Object> map = new HashMap<>();
        map.put("avatar", sysUser.getAvatar());
        map.put("name", sysUser.getUsername());
        return jwtResponse.ok().data(map);
    }


    //pageNo – 当前页
    //pageSize – 每页显示条数
    @GetMapping("userList")
    public JwtResponse getUserList(@RequestParam(value="pageNo", required = false,defaultValue = "1") long pageNo,
                                   @RequestParam(value="pageSize", required = false,defaultValue = "2") long pageSize,
                                   @RequestParam(value="username", required = false) String userName,
                                   @RequestParam(value="mobile", required = false) String mobile,
                                   @RequestParam(value="status", required = false) String isLock){
        System.out.println("用户名:"+userName);
        System.out.println("mobile手机:"+mobile);
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(userName)){
            lqw.like(SysUser::getUsername,userName);
        }
        if(StringUtils.isNotBlank(mobile)){
            lqw.like(SysUser::getMobile,mobile);
        }
        if(StringUtils.isNotBlank(isLock)){
            lqw.eq(SysUser::getStatus,isLock);
        }
        Page<SysUser> sysUserPage = sysUserMapper.selectPage(new Page<SysUser>(pageNo, pageSize), lqw);
        return jwtResponse.ok().data("page",sysUserPage);
    }

    @RequestMapping("/logout")
    public JwtResponse logout() throws Exception {
        return jwtResponse.ok();
    }

    //基于配置的权限控制
    @RequestMapping("/configAuth")
    public ResponseResult test() {
        return new ResponseResult(200, "访问成功");
    }

    //用户status状态更新
    @PutMapping("/putStatus")
    public JwtResponse putStatus(@RequestBody SysUser sysUser){

        try{
            int i = sysUserService.changeStatus(sysUser);
            if(i > 0){
                return jwtResponse.ok();
            }else{
                return jwtResponse.error().message("用户Status更新失败");
            }

        }catch (Exception e){
            // 如果在修改过程中发生任何异常，返回错误响应
            return jwtResponse.error().message("用户状态修改过程中发生错误：" + e.getMessage());
        }


    }

}
